from itertools import product


class PhoneGenerator:
    """Phone number generator class"""

    NUMS = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')

    def __init__(self, country_code: int | str = 7, prefix: int | str = 9) -> None:
        self.prefix = str(country_code)+str(prefix)
        self.gen_len = 10-len(str(prefix))
        self.iter = product(self.NUMS, repeat=self.gen_len)

    def generate(self, chunk_size: int = 10_000):
        """Generate phone numbers iterator with chunks"""

        self.iter_nums = self._produce_chunks(chunk_size)

    def save(self, file_name: str = 'phone_numbers.txt'):
        """Save phone numbers iterator to file"""

        file = open(file_name, 'w')
        for loop, num in enumerate(self.iter_nums):
            print(f'Finished loop {loop} numbers {num[0]}-{num[-1]}')
            file.write('\n'.join(num))
        file.close()

    def _produce_chunks(self, chunk_size: int):
        """Run through iterator and yield array with chunk_size length"""
        arr = []
        chunk = 0
        while True:
            try:
                el = next(self.iter)
                chunk += 1
                arr.append(self.prefix + ''.join(el))
                if chunk == chunk_size:
                    yield arr
                    chunk, arr = 0, []
            except StopIteration:
                if len(arr):
                    yield arr
                break
